package blog.juejin.socket.server;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 验证 socket 的 inputStream.read()会阻塞
 * 
 * author : yeswater
 * date : 2024/8/8
 */

public class BlockServer {
    
    public static void main(String[] args) {
        try {
            while (true) {
                ServerSocket serverSocket = new ServerSocket(6301);
                Socket socket = serverSocket.accept();
                InputStream inputStream = socket.getInputStream();
                byte[] buffer = new byte[1024];
                // 输入留 read 返回的长度
                int length; 
                // inputStream.read(buffer) 会阻塞 socket
                while ((length = inputStream.read(buffer)) != -1) {
                    System.out.println("length : " + length);
                    System.out.println("content " + new String(buffer, 0, length));
                }
                System.out.println("end");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        
    }
    
}
